package cn.xqj.pet.service.impl;

import cn.xqj.basic.exception.BusinessException;
import cn.xqj.basic.util.BaiduAuditUtils;
import cn.xqj.basic.util.getLogininfo;
import cn.xqj.org.domain.Employee;
import cn.xqj.org.mapper.EmployeeMapper;
import cn.xqj.org.service.IEmployeeService;
import cn.xqj.pet.domain.Pet;
import cn.xqj.pet.domain.PetDetail;
import cn.xqj.pet.domain.PetOnlineAuditLog;
import cn.xqj.pet.domain.PetType;
import cn.xqj.pet.mapper.PetDetailMapper;
import cn.xqj.pet.mapper.PetMapper;
import cn.xqj.pet.mapper.PetOnlineAuditLogMapper;
import cn.xqj.pet.service.IPetService;
import cn.xqj.basic.service.impl.BaseServiceImpl;
import cn.xqj.basic.util.getLogininfo;
import cn.xqj.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.security.auth.login.LoginContext;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * 业务实现类：
 */
@Service
public class PetServiceImpl extends BaseServiceImpl<Pet> implements IPetService {


    @Autowired
    private PetDetailMapper petDetailMapper;

    @Autowired
    private PetMapper petMapper;

    @Autowired
    private PetOnlineAuditLogMapper petOnlineAuditLogMapper;

    @Autowired
    private IEmployeeService employeeService;

    @Autowired
    private EmployeeMapper employeeMapper;


    @Transactional
    @Override
    public void add(Pet pet) {
        super.add(pet);

        PetDetail petDetail = pet.getPetDetail();
        if (petDetail != null) {
            petDetail.setPetId(pet.getId());
            petDetailMapper.add(petDetail);
        }
    }

    @Override
    public void update(Pet pet) {
        super.update(pet);
        PetDetail petDetail = pet.getPetDetail();
        if (petDetail != null) {
            petDetail.setPetId(pet.getId());
            petDetailMapper.updatePetDetailByPetId(petDetail);
        }
    }

    @Transactional
    @Override
    public void del(Long id) {
        super.del(id);
        petDetailMapper.delPetDetailByPetId(id);
    }


    @Override
    public void onsale(Long[] ids, HttpServletRequest request) {

        Logininfo logininfo = getLogininfo.getLogininfo(request);
        Date onsaletime = new Date();

        for (Long id : ids) {
            Pet pet = petMapper.findOne(id);
            // Boolean textCensor = BaiduAuditUtils.TextCensor(pet.getName());
            Boolean imgCensors = BaiduAuditUtils.ImgCensors(pet.getResources());
            PetOnlineAuditLog petOnlineAuditLog = new PetOnlineAuditLog();
            petOnlineAuditLog.setPetId(pet.getId());
            Employee employee = employeeMapper.findByLogininfoId(logininfo.getId());
            petOnlineAuditLog.setAuditId(employee.getId());
            petOnlineAuditLog.setAuditTime(new Date());
            if (true && imgCensors) {
                pet.setState(0);
                pet.setOnsaletime(onsaletime);
                petMapper.update(pet);
                petOnlineAuditLog.setState(1);
                petOnlineAuditLog.setNote("审核成功");
            } else {
                petOnlineAuditLog.setState(-1);
                petOnlineAuditLog.setNote("审核失败");
                throw new BusinessException("审核失败,含有非法字符或图片！");
            }
            petOnlineAuditLogMapper.add(petOnlineAuditLog);
        }


    }

    @Override
    public void offsale(Long[] ids) {
        Date offsaletime = new Date();
        petMapper.offsale(ids, offsaletime);
    }

}
